package com.xbongbong.workorder.permission;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.abstracts.AbstractBusinessPermissionService;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.WorkOrderFormEntity;
import com.xbongbong.saas.enums.WorkOrderTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderCcModel;
import com.xbongbong.saas.model.WorkOrderFlowNodeModel;
import com.xbongbong.saas.model.WorkOrderFlowUserModel;
import com.xbongbong.saas.model.WorkOrderModel;
import com.xbongbong.sys.enums.RoleEnum;
import com.xbongbong.sys.util.UserPermissionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/27 10:29
 */
@Service("workOrderPermissionServiceImpl")
public class WorkOrderPermissionServiceImpl extends AbstractBusinessPermissionService< WorkOrderEntity, WorkOrderFormEntity> {
    @Resource
    private WorkOrderModel workOrderModel;
    @Resource
    private WorkOrderCcModel workOrderCcModel;
    @Resource
    private WorkOrderFlowNodeModel workOrderFlowNodeModel;
    @Resource
    private WorkOrderFlowUserModel workOrderFlowUserModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER.getCode());
    }




    @Override
    public void checkPermission( WorkOrderFormEntity formEntity , WorkOrderEntity paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (hasPermission(formEntity, paasFormDataEntityExt, loginUser, subBusinessType)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
    }

    @Override
    public boolean hasPermission(WorkOrderFormEntity formEntity, WorkOrderEntity paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (UserPermissionUtil.isMaxDataPermission(loginUser, SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), DistributorMarkEnum.OTHER.getCode(), formEntity.getMenuId(), false)){
            return true;
        }
        Long dataId = paasFormDataEntityExt.getId();
        if (Objects.equals(subBusinessType, XbbRefTypeEnum.WORK_ORDER_ROB.getCode())) {
            return true;
        }
        if ( loginUser.getRoleIds().contains(RoleEnum.WORKORDER_MANAGER.getAlias())) {
            return true;
        }
        if (Objects.equals(paasFormDataEntityExt.getCreatorId(), loginUser.getUserId())) {
            return true;
        }
        WorkOrderEntity workOrderEntity = workOrderModel.getByKey(dataId, loginUser.getCorpid());
        if (Objects.equals(workOrderEntity.getType(), WorkOrderTypeEnum.ROB.getCode())) {
            return true;
        }
        List<String> userIdIn = getUserIdIn(loginUser);
        Integer count;
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",loginUser.getCorpid());
        param.put("workOrderId", dataId);
        param.put("userIdIn",userIdIn);
        param.put("del",0);
        count = workOrderCcModel.getEntitysCount(param);
        if (count > 0){
            return true;
        }
        count = workOrderFlowNodeModel.getEntitysCount(param);
        if (count > 0){
            return true;
        }
        param.put("dataId",dataId);
        count = workOrderFlowUserModel.getEntitysCount(param);
        return count == 0;
    }
}
